import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;

import opennlp.tools.chunker.ChunkSample;
import opennlp.tools.chunker.ChunkerME;
import opennlp.tools.chunker.ChunkerModel;
import opennlp.tools.cmdline.chunker.ChunkerMETool;
import opennlp.tools.postag.POSSample;
import opennlp.tools.tokenize.WhitespaceTokenizer;
import opennlp.tools.util.InvalidFormatException;


public class chunkerGeneration {

	/**
	 * @param args
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws InvalidFormatException 
	 */
	public static void main(String[] args) throws InvalidFormatException, FileNotFoundException, IOException {
		// TODO Auto-generated method stub
		
		String directoryName = "/home/pjbarrio/Dataset/ACE2003/ace_phase2/data/train/";
		
		File directory = new File(directoryName);
		
		File[] files = directory.listFiles();
		
		ChunkerME c = new ChunkerME(new ChunkerModel(new FileInputStream(new File("/home/pjbarrio/Software/openNLP/resources/en-chunker.bin"))));
		
		for (File file : files) {
			
			if (isPos(file)){
			
				System.setOut(new PrintStream(new File(file.getAbsolutePath().replace(".pos", ".chk"))));
				
				BufferedReader br = new BufferedReader(new FileReader(file));
			
				String line = br.readLine();
				
				while (line!=null){
					
					if (line.trim().equals("")){
						System.out.println("");
					} else {
						line = getParseableLine(line);
						POSSample posSample;
				        try {
				          posSample = POSSample.parse(line);
				        } catch (InvalidFormatException e) {
				        	System.err.println(file.getName());
							
				        	System.err.println("Invalid format:");
				        	System.err.println(line);
				        	System.err.println("reprocessing...");
				          
				        	line = reformat(line);

				        	continue;
				        }
				        
				        String[] chunks = c.chunk(posSample.getSentence(),
				            posSample.getTags());
				        
				        System.out.println(new ChunkSample(posSample.getSentence(),
				            posSample.getTags(), chunks).nicePrint());
				        
					}
					
					line = br.readLine();
					
				}
				
				br.close();
			}
		}
		
	}

	private static String reformat(String sentenceString) {
		
		String tokenTags[] = WhitespaceTokenizer.INSTANCE.tokenize(sentenceString);
		
		String ret = "";
		
		for (int i = 0; i < tokenTags.length; i++) {
			
			int split = tokenTags[i].lastIndexOf("_");

			if (split == -1) {
				
				tokenTags[i+1] = tokenTags[i] + tokenTags[i+1];
				
			} else {
				
				ret = ret + " " + tokenTags[i];
				
			}
			
		}
		
		return ret.substring(1);
		
	}

	private static String getParseableLine(String line) {
		
		line = line.replace("\\/", "@&@#&#$&$#&#@&@");
		
		line = line.replace("/", "_");
		
		return line.replace("@&@#&#$&$#&#@&@", "\\/");
		
	}

	private static boolean isPos(File file) {
		return file.getName().endsWith(".pos");
	}

}
